/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Map;

import IO.Utils;
import Tactics.TackticUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import net.slashie.libjcsi.CSIColor;

/**
 *
 * @author insane
 */
public class MapUtils {

    public static boolean checkMove(char[][] ismap, int x, int y) throws FileNotFoundException, IOException, ClassNotFoundException {
        char check[] = {'#', '+'};
        for (int i = 0; i < check.length; i++) {
            if (ismap[x][y] == check[i] ) {
                return true;
            }
        }
        return false;
    }

    public static boolean checkTrans(char[][] ismap, int x, int y) {
        char check[] = {'#', '+'};

        for (int i = 0; i < check.length; i++) {
            if (ismap[x][y] == check[i]) {
                return true;
            }
        }
        return false;
    }

    public static CSIColor getColor(int i) {
        switch (i) {
            case 0:
                return CSIColor.BLACK;
            case 1:
                return CSIColor.BROWN;
            case 2:
                return CSIColor.SILVER;
            case 3:
                return CSIColor.WHITE;
            case 4:
                return CSIColor.RED;
            case 5:
                return new CSIColor().getColorFromCode(2); //green
            case 6:
                return new CSIColor().getColorFromCode(1); //blue
        }
        return null;
    }

    public static int[] ViewToScreen(int x, int y, Map map) {
        if (x >= map.x - MapConfig.getInstance().getViewSizeWidth()
                && x <= map.x + MapConfig.getInstance().getViewSizeWidth()
                && y >= map.y - MapConfig.getInstance().getViewSizeHeight()
                && y <= map.y + MapConfig.getInstance().getViewSizeHeight() - 1) {

            int startx = map.x - MapConfig.getInstance().getViewSizeWidth();
            int starty = map.y - MapConfig.getInstance().getViewSizeHeight();

            for (int xx = 0; xx < MapConfig.getInstance().getViewSizeWidth() * 2; xx++) {
                for (int yy = 0; yy < MapConfig.getInstance().getScreenHeight() * 2; yy++) {
                    if (startx + xx == x && starty + yy == y) {
                        int[] position = {xx, yy};
                        return position;
                    }
                }
            }
        }
        return null;
    }

    public static int[] getEmptyPosition(Map map) throws FileNotFoundException, IOException, ClassNotFoundException {
        boolean exit = true;
        while (exit) {
            int x = Utils.rand(2, map.getWidth() - 2);
            int y = Utils.rand(2, map.getHeight() - 2);
            if (!checkMove(map.getMap(), x, y)) {
                int[] position = {x, y};
                return position;
            }
        }
        return null;
    }

    public static boolean checkFovVisible(int x, int y) throws FileNotFoundException, IOException, ClassNotFoundException {
        boolean[][] fovcolor = MapLevel.getInstance().getMap().getFovcolor();
        int fov = MapConfig.getInstance().getFov();

        int xx = MapConfig.getInstance().getViewSizeWidth();
        int yy = MapConfig.getInstance().getViewSizeHeight();
        int startx = xx - fov * 2;
        int starty = yy - fov;
        for (int w = 0; w <= fov * 4; w++) {
            for (int h = 0; h <= fov * 2; h++) {
                if (fovcolor[w][h] && x == w + startx && h + starty == y) {
                    return true;
                }
            }
        }
        return false;
    }
}
